package com.auto.api.core.type.classtomap;

import com.auto.api.core.entity.ClassToMapContext;
import com.auto.api.core.entity.ProcessProp;
import com.auto.api.core.entity.PropertyContext;
import com.auto.api.core.type.TypeFactory;
import com.auto.api.core.util.BeanUtil;
import java.beans.PropertyDescriptor;
import java.util.Map;

/**
 * @program:
 * @description: Class类型转换解析YaPi结果集返回json
 * @author: liangshf
 * @create: 2020-09-24
 */
public class ClassToResultApiMapHandler extends AbstractClassToMapHandler {

    @Override
    public ProcessProp<ClassToMapContext> getProcessProp(ClassToMapContext context, PropertyContext propertyContext) {
        return new ProcessProp(propertyContext.getJudgePropType(), context, propertyContext);
    }

    public void processProp(ProcessProp<ClassToMapContext> processProp) {
        PropertyDescriptor prop = processProp.getPropertyContext().getProp();
        //多个继承泛型替换
        if (TypeFactory.isSimpleTypeHandler(processProp.getPropType())) {
            if (processProp.getPropertyContext().isCollectionType()) {
                BeanUtil.getListField(processProp.getContext().getClazz(), processProp.getContext().getResult(), prop.getName());
                return;
            }
            BeanUtil.getFiledApi(processProp.getContext().getClazz(), processProp.getContext().getResult(), prop);
        } else {
            if (processProp.getPropertyContext().getPropResType().isAssignableFrom(Map.class)) {
                System.out.println(11111);
            }
            Map<String, Object> columnMap = getResult(new ClassToMapContext(processProp.getPropertyContext().isObjParse() ? processProp.getPropertyContext().getGenPropResType() : processProp.getPropertyContext().getPropResType()));
            processProp.getContext().getResult()
                .put(prop.getName()
                    , processProp.getPropertyContext().isCollectionType() ? BeanUtil.getApiJsonArray(processProp.getContext().getClazz(), prop, columnMap)
                                                                          : BeanUtil.getObjApi(processProp.getContext().getClazz(), prop, columnMap)
                );
        }
    }

}
